//
//  LSExponentViewController.m
//  LookScore
//
//  Created by wr on 2018/3/20.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSExponentViewController.h"
#import "CustomSegmentedView.h"

#import "LSExponentSectionHeaderView.h"
#import "LSPageView.h"
#import "LSExponentView.h"
#import "LSExponentModel.h"
@interface LSExponentViewController ()<LSExponentSectionHeaderViewDelegate>

@property (nonatomic, strong) NSArray * titlesArray;
@property (nonatomic, strong) LSExponentView * initialView;
@property (nonatomic, strong) LSPageView * pageView;
@property (nonatomic, strong) NSMutableArray * modelArray;

@property (nonatomic, assign) NSInteger selectType;
@end          

@implementation LSExponentViewController{
    LSExponentSectionHeaderView * _headerView;
   
}

- (NSMutableArray *)modelArray{
    if (!_modelArray) {
        _modelArray = @[].mutableCopy;
    }
    
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlesArray = @[@"欧赔",@"亚盘",@"大小球"];
    [self.view addSubview: [self tableHeadView]];
    [self loadData];
    
    
}

- (void)loadData{
//    @{@"matchId":@(self.macthId)}
    [LSHttpEnclosureTool GET:DATABASEINDEXDETAIL parameters:@{@"matchId":@(self.macthId)} resBlock:^(id responseObject) {
        if (responseObject) {
            [self.modelArray removeAllObjects];
//            for (NSDictionary * dict in responseObject) {
            NSDictionary * dict = responseObject;
                NSMutableDictionary * dict1 = dict.mutableCopy;
                [self arrangementData:dict1 withArrayNameStr:@"AIHistoricaDataList"];
                [self arrangementData:dict1 withArrayNameStr:@"OddsChangeRecordList"];
                [self arrangementData:dict1 withArrayNameStr:@"SDHistoricaDataList"];
                LSExponentModel * model = [LSExponentModel mj_objectWithKeyValues:dict1];
                [self.modelArray addObject:model];
            MyLog(@"%@",dict1[@"OddsChangeRecordList"]);
            self.initialView.selectType = _selectType;
            self.pageView.selectType = _selectType;
            switch (_selectType) {
                case 0:{
                    self.initialView.dataArray = model.OddsChangeRecordList.mutableCopy;
                    self.pageView.dataArray = model.OddsChangeRecordList.mutableCopy;
                    
                }
                    break;
                case 1:{
                    self.initialView.dataArray = model.AIHistoricaDataList.mutableCopy;
                    self.pageView.dataArray = model.AIHistoricaDataList.mutableCopy;
                }
                    break;
                    
                case 2:{
                    
                    self.initialView.dataArray = model.SDHistoricaDataList.mutableCopy;
                    self.pageView.dataArray = model.SDHistoricaDataList.mutableCopy;
                }
                    break;
                default:
                    break;
            }
            
//                MyLog(@"%@",dict1);
//            }
            
           
        }else{
            
        }
    }];
    
    
}
- (void)arrangementData:(NSMutableDictionary *)responseObject withArrayNameStr:(NSString *)name{
    
    NSMutableArray * array = [NSMutableArray arrayWithArray:responseObject[name]];
    NSMutableArray *dateMutablearray = [@[] mutableCopy];
    
    for (NSInteger i = 0; i<array.count; i++) {
        NSMutableDictionary * iDict = array[i];
        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:iDict];
        NSMutableArray *tempArray = [@[] mutableCopy];
        [tempArray addObject:dict];
        for (NSInteger j = i + 1; j < array.count; j++) {
            NSDictionary * arrrayDict = array[j];
            NSMutableDictionary * dict1 = [NSMutableDictionary dictionaryWithDictionary:arrrayDict] ;
            if ([CommonClass isBlankString:dict1[@"CompanyName"]]) {
//                dict1[@"CompanyName"] = @"";
                [dict1 setObject:@"" forKey:@"CompanyName"];
            }
            if ([CommonClass isBlankString:dict[@"CompanyName"]]) {
//                dict[@"CompanyName"] = @"";
                [dict setObject:@"" forKey:@"CompanyName"];
            }
            
            
            
            if ([dict[@"CompanyId"] integerValue] == [dict1[@"CompanyId"] integerValue] && [dict[@"CompanyName"] isEqualToString:dict1[@"CompanyName"]] && [dict[@"MatchId"] integerValue] == [dict1[@"MatchId"] integerValue]) {
                [tempArray addObject:dict1];
                [array removeObjectAtIndex:j];
                j -= 1;
            }
        }
        [dateMutablearray addObject:@{@"companyData":tempArray,@"CompanyId":dict[@"CompanyId"],@"CompanyName":dict[@"CompanyName"],@"MatchId":dict[@"MatchId"]}];
    }
    
    [responseObject setObject:dateMutablearray forKey:name];
    
}

- (UIView *)tableHeadView{
    
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, LSScreenHeight - 200)];
    backView.backgroundColor=LSTitleWhiteColor;
    
    CustomSegmentedView *aCustomSegmentedView=[[CustomSegmentedView alloc]initWidthCustomSegmentedViewFrame:CGRectMake((LSScreenWidth-180)/2, 10, 180, 24) titlesArray:self.titlesArray seletedTitleColor:[UIColor whiteColor] normalTitleColor:LSSubColorFF9E44 seletedBgColor:LSSubColorFF9E44 normalBgColor:[UIColor whiteColor] bordeColor:LSSubColorFF9E44  borderWidth:1 seletedCustomViewIndex:^(NSInteger index) {
        _selectType = index;
        LSExponentModel * model = self.modelArray.firstObject;
        self.initialView.selectType = _selectType;
        self.pageView.selectType = _selectType;
        _headerView.type = index;
        self.pageView.selectIndex = 0;
        switch (index) {
                
            case 0:{
                //欧盘
                self.initialView.dataArray = model.OddsChangeRecordList.mutableCopy;
                self.pageView.dataArray = model.OddsChangeRecordList.mutableCopy;
            }
                break;
            case 1:{
                //亚盘
                self.initialView.dataArray = model.AIHistoricaDataList.mutableCopy;
                self.pageView.dataArray = model.AIHistoricaDataList.mutableCopy;
            }
                break;
                
            case 2:{
                self.initialView.dataArray = model.SDHistoricaDataList.mutableCopy;
                self.pageView.dataArray = model.SDHistoricaDataList.mutableCopy;
            }
                break;
            default:
                break;
        }
        
    }];
    [aCustomSegmentedView selectedDefaultSegmentedIndex:1];
    _selectType = 1;
    
    _headerView = [[LSExponentSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    _headerView.delegate = self;
    _headerView.isHiddening = 0;
    [backView addSubview:aCustomSegmentedView];
    _headerView = [[LSExponentSectionHeaderView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 40)];
    _headerView.backgroundColor = LSBackGroundColor;
    _headerView.delegate = self;
    _headerView.isHiddening = 0;
    [backView addSubview:_headerView];
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 84, LSScreenWidth,.5f)];
    lineView.backgroundColor=LSLineGrayColor;
    [backView addSubview:lineView];
    self.initialView = [[LSExponentView alloc] initWithFrame:CGRectMake(0, 84, LSScreenWidth, LSScreenHeight-64)];
    self.initialView.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSelf = self;
    weakSelf.initialView.block = ^(NSIndexPath *indexPath,NSMutableArray * dataArray) {
        _headerView.isHiddening = 1;
        self.initialView.hidden = YES;
        self.pageView.hidden = NO;
        self.pageView.selectIndex = indexPath.row;
        self.pageView.dataArray = dataArray;
    };
//    self.initialView.hidden = YES;
    [backView addSubview:self.initialView];
    
    self.initialView.sd_layout
    .leftEqualToView(self.view)
    .topSpaceToView(_headerView, 0)
    .rightEqualToView(self.view)
    .heightIs(LSScreenHeight-LSNavBarHeight-84-35);
    
    self.pageView = [[LSPageView alloc] initWithFrame:CGRectMake(0, 84, LSScreenWidth, LSScreenHeight-200)];
    self.pageView.backgroundColor = [UIColor whiteColor];
    [backView addSubview:self.pageView];
    
    self.pageView.sd_layout
    .leftEqualToView(self.view)
    .topSpaceToView(_headerView, 0)
    .rightEqualToView(self.view)
    .heightIs(LSScreenHeight-35);
    self.pageView.hidden = YES;
    
    
    return backView;
    
}




#pragma mark -- LSExponentSectionHeaderViewDelegate
- (void)backBtnClicked:(UIButton *)sender{
    
    _headerView.isHiddening = 0;
    self.initialView.hidden = NO;
    self.pageView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
